package com.css.dzj.dzjjcss.modules.todo.service.impl;

import com.css.dzj.dzjjcss.common.util.Constant;
import com.css.dzj.dzjjcss.common.util.R;
import com.css.dzj.dzjjcss.modules.todo.entity.JcssTodoEntity;
import com.css.dzj.dzjjcss.modules.todo.entity.QJcssTodoEntity;
import com.css.dzj.dzjjcss.modules.todo.mapper.TodoMapper;
import com.css.dzj.dzjjcss.modules.todo.service.TodoService;
import com.querydsl.core.QueryResults;
import com.querydsl.core.types.ExpressionUtils;
import com.querydsl.core.types.Predicate;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class TodoServiceImpl implements TodoService {
    @Autowired
    private TodoMapper todoMapper;
    @Autowired
    private JPAQueryFactory jpaQueryFactory;
    @Override
    @Transactional
    public R add(JcssTodoEntity jcssTodoEntity) {
        try {
            todoMapper.save(jcssTodoEntity);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        }
    }

    @Override
    @Transactional
    public R delete(String id) {
        try {
            if(!"".equals(id) && id!=null){
                QJcssTodoEntity q = QJcssTodoEntity.jcssTodoEntity;
                jpaQueryFactory.update(q)
                        .set(q.delFlag, Constant.DELETE)
                        .where(q.id.eq(id))
                        .execute();
                System.out.println();
                return R.ok();
            }else {
                throw new RuntimeException();
            }
        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        }
    }

    @Override
    @Transactional
    public String update(JcssTodoEntity jcssTodoEntity) {
        return null;
    }

    @Override
    @Transactional
    public JcssTodoEntity selectOne(String id) {
        try {
            JcssTodoEntity jcssTodoEntity =  todoMapper.getOne(id);
            return jcssTodoEntity;
        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        }
    }

    @Override
    @Transactional
    public QueryResults<JcssTodoEntity> selectAll(Pageable pageable, String title) {
        QJcssTodoEntity q = QJcssTodoEntity.jcssTodoEntity;
        Predicate predicate = q.isNotNull().or(q.isNull());
        predicate = (title == null || title.isEmpty()) ? predicate : ExpressionUtils.and(predicate, q.title.like("%" + title + "%"));
        predicate = ExpressionUtils.and(predicate, q.delFlag.eq(Constant.NO_DELETE));
        return jpaQueryFactory
                .selectFrom(q)
                .where(
                        predicate
                )
                .orderBy(q.createTime.desc())
                .offset(pageable.getOffset())   //起始页
                .limit(pageable.getPageSize())  //每页大小
                .fetchResults();
    }


    @Override
    @Transactional
    public QueryResults<JcssTodoEntity> queryAll(Pageable pageable,String orgName) {
        QJcssTodoEntity q = QJcssTodoEntity.jcssTodoEntity;
        Predicate predicate = q.isNotNull().or(q.isNull());
        predicate = ExpressionUtils.and(predicate, q.delFlag.eq(Constant.NO_DELETE));
        predicate = (orgName == null || orgName.isEmpty()) ? predicate : ExpressionUtils.and(predicate, q.orgName.like("%" + orgName + "%"));
        return jpaQueryFactory
                .selectFrom(q)
                .where(
                        predicate
                )
                .orderBy(q.createTime.desc())
                .offset(pageable.getOffset())   //起始页
                .limit(pageable.getPageSize())  //每页大小
                .fetchResults();
    }
}
